;*******************************************************************************
; Title:	IfCommand
; Author:	Philip Ludlam
; Copyright:	(C) Philip Ludlam 2006
; Version:	1.01, 7 May 2006
;
; Changes:	Updated for the A9home by Philip Ludlam
;
;*******************************************************************************
; This program is free software; you can redistribute it and/or modify it
; under the terms of the GNU General Public License as published by the Free
; Software Foundation; either version 2 of the License, or (at your option)
; any later version.
;
; This program is distributed in the hope that it will be useful, but WITHOUT
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
; FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more
; details.
;
; You should have received a copy of the GNU General Public License along with
; this program; if not, write to the Free Software Foundation, Inc., 59 Temple
; Place - Suite 330, Boston, MA 02111-1307, USA
;
;*******************************************************************************


		TTL	> IfCommand

		GET	OSLib:oslib.hdr.OS
		GET	OSLib:oslib.hdr.OSFile
		GET	OSLib:oslib.hdr.OSFSControl
		GET	AsmLib2:hdr.RegsBoth

arg_file	*	&0
arg_command	*	&4
arg_yes		*	&8
arg_no		*	&C


		AREA	|main|, CODE, READONLY

		ENTRY

;-------------------------------------------------------------------------------

Start		B	Entry			; Entry instruction
		DCD	&79766748		; Magic value 1
		DCD	&216C6776		; Magic value 2
		DCD	End-Start		; Read only size
		DCD	0			; Read/write size
		DCD	32			; 26 or 32 bit build

Entry		MOV	r11, lr			; Store lr in r11

		ADR	r0, args
		MOV	r2, r12			;  to workspace
		SUB	r3, r13, r12		; r3 = size in workspace
		SWI	XOS_ReadArgs
		ADRVS	r0, syntax
		MOVVS	pc, r11

		LDR	r2, [r12, #arg_command]
		CMP	r2, #0
		BEQ	syntax_code

		SUB	r8, r13, r3		; r8 = next free block in workspace

do
		MOV	r5, r3			; r5 = space left in workspace

		MOV	r0, #OSFSControl_SetTemporaryFS	; r0 = 11
		ADR	r1, null
		SWI	XOS_FSControl
		MOVVS	pc, r11

		MOV	r0, #OSFSControl_CanonicalisePath	; r0 = 37
		LDR	r1, [r12, #arg_file]
		MOV	r2, r8
		ADR	r3, runpath
		MOV	r4, #0
		SWI	XOS_FSControl
		MOVVS	pc, r11

		MOV	r0, #OSFile_ReadStampedNoPath	; r0 = 23
		MOV	r1, r8
		SWI	XOS_File
		MOVVS	pc, r11

		CMP	r0, #OSFile_IsFile		; cmp r0, #1
		BEQ	do_command_yes			; found it, so skip

check_alias_vars
		ADR	r0, alias
		MOV	r1, r8
		MOV	r2, #0
		BL	copy
		SUB	r1, r1, #2			; go back two spaces
		LDR	r0, [r12, #arg_file]
		BL	copy
		MOV	r0, r8
		MOV	r1, r8
		MOV	r2, #-1
		MOV	r3, #0
		MOV	r4, #0
		SWI	XOS_ReadVarVal			; ignore any errors
		CMP	r2, #0
		BEQ	do_command_no


do_command_yes
		LDMIA	wp, {r0, r5, r6, r7}
		TEQ	r7, #0
		MOVEQ	r6, #0
		B	do_command

do_command_no
		LDMIA	wp, {r0, r5, r6, r7}
		TEQ	r6, #0
		TEQNE	r7, #0
		MOVNE	r6, r7
		BNE	do_command
		TEQ	r6, #0
		BEQ	exit
		MOV	r5, r6
		MOV	r6, #0

do_command
		MOV	r1, r8
		MOV	r0, r5
		MOV	r2, #32
		BL	copy
		MOVS	r0, r6
		MOV	r2, #0
		BLNE	copy
		STREQB	r2, [r1, #-1]

os_cli
		MOV	r0, r8
		SWI	XOS_CLI

exit
		MOV	pc, r11

copy
		LDRB	r3, [r0], #1
		STRB	r3, [r1], #1
		CMP	r3, #32
		BGE	copy
		STRB	r2, [r1, #-1]
		MOV	pc, lr

syntax_code
		SetV
		ADR	r0, syntax
		MOV	pc, r11

syntax
		DCD	0
		DCB	"Syntax: *IfCommand <file> <yes command> [<no command>]", 10, 13
		DCB	"or:     *IfCommand <file> <command> <yes param> <no param>"
		DCB	0
		ALIGN

args
		DCB	",,,"
		DCB	0
		ALIGN

null
		DCB	"Null:", 0
		ALIGN

runpath
		DCB	"Run$Path", 0
		ALIGN

alias
		DCB	"Alias$ "
;		ALIGN

Licence		DCB	" PJL, 2002. Licence: GPL."
		ALIGN

End
		END
